<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name'}"
      show-checkbox
      check-strictly
      auto-expand-parent
      node-key="id"
    />
    <div style="text-align: right;">
      <el-button @click="$emit('successs')">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </div>
</template>

<script>
import { transDataToTreeData } from '@/utils'
import { getPermissionList } from '@/api/permisson'
import { assignPrem, updateRoles } from '@/api/settings'
export default {
  name: 'VueHr3Assignpermission',
  props: {
    culId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  created() {
    this.loadPermission()
  },
  methods: {
    // 给角色分配权限
    async submit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        await assignPrem({ id: this.culId, permIds: permIds })
        this.$emit('success')
      } catch (error) {
        console.log(error)
      }
    },
    // 获取所有权限列表
    async loadPermission() {
      try {
        const res = await getPermissionList()
        // console.log(res)
        // // 获取点击的角色已经拥有的权限
        const detail = await updateRoles(this.culId)
        // console.log(detail)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)
        this.list = transDataToTreeData(res.data)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
